package com.ocom.payconfig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.GetOrderMealLeaveRequest;
import com.ocom.common.request.payconfig.MealLeaveReports;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.StringUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfMealLeave;
import com.ocom.payconfig.service.XfMealLeaveService;
import com.ocom.common.entity.payconfig.EntityXfMealLeave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class XfMealLeaveServiceImpl extends ServiceImpl<IDaoXfMealLeave, EntityXfMealLeave> implements XfMealLeaveService {


    @Autowired
    private IDaoXfMealLeave iDaoXfMealLeave;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfMealLeave> queryMealLeave(Long companyId, MealLeaveReports mealLeaveReports) {
        LambdaQueryWrapper<EntityXfMealLeave> appUserQueryWrapper = Wrappers.lambdaQuery();
        appUserQueryWrapper.eq(mealLeaveReports.getDeptId() != null, EntityXfMealLeave::getDeptId, mealLeaveReports.getDeptId())
                .eq(mealLeaveReports.getIsLeave() != null, EntityXfMealLeave::getIsLeave, mealLeaveReports.getIsLeave())
                .likeLeft(StringUtil.isNotEmpty(mealLeaveReports.getPName()), EntityXfMealLeave::getpName, mealLeaveReports.getPName())
                .likeLeft(StringUtil.isNotEmpty(mealLeaveReports.getPCode()), EntityXfMealLeave::getpCode, mealLeaveReports.getPCode())
                .eq(mealLeaveReports.getMealNo() != null, EntityXfMealLeave::getMealNo, mealLeaveReports.getMealNo())
                .ge(StringUtil.isEmpty(mealLeaveReports.getEndDate()), EntityXfMealLeave::getMealDate, mealLeaveReports.getBeginDate())
                .between(StringUtil.isNotBlank(mealLeaveReports.getEndDate()), EntityXfMealLeave::getMealDate,
                        mealLeaveReports.getBeginDate(), mealLeaveReports.getEndDate());

        List<EntityXfMealLeave> list = this.list(appUserQueryWrapper);
        return list;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfMealLeave> getOrderMealLeave(Long companyId, GetOrderMealLeaveRequest getOrderMealLeaveRequest) {
        LambdaQueryWrapper<EntityXfMealLeave> xfMealLeaveQueryWrapper = Wrappers.lambdaQuery();
        xfMealLeaveQueryWrapper.eq(getOrderMealLeaveRequest.getComId() != null, EntityXfMealLeave::getComId, getOrderMealLeaveRequest.getComId())
                .eq(getOrderMealLeaveRequest.getMealNo() != null, EntityXfMealLeave::getMealNo, getOrderMealLeaveRequest.getMealNo())
                .eq(getOrderMealLeaveRequest.getpId() != null, EntityXfMealLeave::getpId, getOrderMealLeaveRequest.getpId())
                .eq(StringUtil.isNotEmpty(getOrderMealLeaveRequest.getpCode()), EntityXfMealLeave::getpCode, getOrderMealLeaveRequest.getpCode())
                .eq(StringUtil.isNotBlank(getOrderMealLeaveRequest.getpName()), EntityXfMealLeave::getpName, getOrderMealLeaveRequest.getpName())
                .eq(getOrderMealLeaveRequest.getDeptId() != null, EntityXfMealLeave::getDeptId, getOrderMealLeaveRequest.getDeptId())
                .eq(getOrderMealLeaveRequest.getIsLeave() != null, EntityXfMealLeave::getIsLeave, getOrderMealLeaveRequest.getIsLeave())
                // 结束时间空走>查询 结束时间不为空走范围查询
                .ge(StringUtil.isEmpty(getOrderMealLeaveRequest.getEndDate()), EntityXfMealLeave::getMealDate, getOrderMealLeaveRequest.getBeginDate())
                .between(StringUtil.isNotEmpty(getOrderMealLeaveRequest.getEndDate()), EntityXfMealLeave::getMealDate,
                        getOrderMealLeaveRequest.getBeginDate(), getOrderMealLeaveRequest.getEndDate())
                .orderByDesc(EntityXfMealLeave::getUpdateTime);
        if (null != getOrderMealLeaveRequest.getPage() && null != getOrderMealLeaveRequest.getSize()) {
            PageHelper.startPage(getOrderMealLeaveRequest.getPage(), getOrderMealLeaveRequest.getSize());
        }
        return this.list(xfMealLeaveQueryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.MASTER)
    public int setOrderMealLeaveRequest(Long companyId, EntityXfMealLeave xfMealLeave) throws ParseException {
        // 获取当前时间
        String dateTime = DateUtil.getDateTime();
        Date createTime = DateUtil.getDate(dateTime, "yyyy-MM-dd HH:mm:ss");
        if (xfMealLeave.getIsLeave() == 1) {
            xfMealLeave.setCreateTime(createTime);
            boolean save = this.save(xfMealLeave);
            if (!save) {
                return 1;
            }
        }
        if (xfMealLeave.getIsLeave() == 0) {
            UpdateWrapper<EntityXfMealLeave> xfMealLeaveUpdateWrapper = Wrappers.update();
            xfMealLeaveUpdateWrapper.set("is_leave", xfMealLeave.getIsLeave())
                    .set("update_time", createTime)
                    .eq("com_id", xfMealLeave.getComId())
                    .eq("p_id", xfMealLeave.getpId())
                    .eq("meal_date", xfMealLeave.getMealDate())
                    .eq("meal_no", xfMealLeave.getMealNo())
                    .eq("is_leave", 1);
            boolean save = this.update(xfMealLeaveUpdateWrapper);
            if (!save) {
                return 1;
            }
        }

        return 0;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public int perLeaveDays(Long companyId, Long pId ,Integer mealNo, List<String> days) {
        LambdaQueryWrapper<EntityXfMealLeave> xfMealLeaveQueryWrapper = Wrappers.lambdaQuery();
        xfMealLeaveQueryWrapper.eq(EntityXfMealLeave::getComId,companyId);
        xfMealLeaveQueryWrapper.eq(EntityXfMealLeave::getpId,pId);
        xfMealLeaveQueryWrapper.in(EntityXfMealLeave::getMealDate,days);
        xfMealLeaveQueryWrapper.eq(EntityXfMealLeave::getMealNo,mealNo);
        xfMealLeaveQueryWrapper.apply("is_leave = 1");
        return this.count(xfMealLeaveQueryWrapper);
    }

}
